#                                               -*- Autoconf -*-
# configure script generation for ncblog
#

AC_PREREQ([2.67])
AC_INIT([ncblog], [0.01], [matzeton@googlemail.com])
AC_CONFIG_AUX_DIR([build])
AM_INIT_AUTOMAKE([1.11 foreign no-define -Wall -Werror])
AM_WITH_DMALLOC
AC_CANONICAL_BUILD
AC_CANONICAL_HOST
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([src/config.h.in])
AC_CONFIG_HEADER([src/config.h])
AC_USE_SYSTEM_EXTENSIONS
CFLAGS=

# Checks for programs.
AM_PROG_AR
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_C_INLINE
AC_PREFIX_DEFAULT([/usr])

# Checks for header files.
AC_HEADER_SYS_WAIT
AC_HEADER_TIME
AC_HEADER_STDBOOL
AC_HEADER_STDC
AC_HEADER_STAT
AC_HEADER_DIRENT
AC_HEADER_ASSERT
AC_CHECK_HEADERS([fcntl.h limits.h stdint.h sys/param.h sys/socket.h sys/stat.h sys/types.h termios.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_COMPUTE_INT
AC_TYPE_UID_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINT16_T
AC_TYPE_UINT8_T
AC_TYPE_LONG_LONG_INT
AC_TYPE_UNSIGNED_LONG_LONG_INT
AX_CHECK_TYPEDEF([va_list], [stdarg.h],,[AC_MSG_ERROR([*** Missing va_list typedef in stdarg.h])])
AX_CHECK_TYPEDEF([size_t], [stdio.h],,[AC_MSG_ERROR([*** Missing size_t typedef in stdio.h])])

# Checks for library functions.
AC_FUNC_CHOWN
if test "x$ac_cv_func_chown_works" = "x"; then
  AC_MSG_ERROR([*** missing chown(..,) function])
fi
AC_FUNC_FORK
AC_FUNC_MMAP
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_FUNC_STRNLEN
AC_FUNC_MALLOC
AC_FUNC_STAT
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([dup2 access open close fork umask chdir setsid gettimeofday memmove memcpy memset munmap realpath socket strchr strdup strerror strrchr strstr strlen strnlen asprintf fprintf],,[AC_MSG_ERROR([*** Missing essential functions.])])
AC_CHECK_FUNCS([prctl])
AC_PROG_LN_S

#AX_PROG_LUA
LIBS=""
LUA_VERSION="5.1"
AX_LUA_HEADERS
AX_LUA_LIBS
AM_CONDITIONAL([BUILD_LUA],[false])
AC_ARG_ENABLE([lua], [AS_HELP_STRING([--disable-lua],[Disable lua scripting. Default is enabled if found.])],[have_lua="no"],[
if test "x$_ax_found_lua_libs" = 'xyes' ; then
  have_lua="yes (${LUA_VERSION})"
  AM_CONDITIONAL([BUILD_LUA],[true])
  AC_DEFINE([ENABLE_LUA],[1],[Enable LUA template scripting])
  LIBS="${LIBS} ${LUA_LIB}"
else
  have_lua="no"
fi])

AC_ARG_ENABLE([summary], [AS_HELP_STRING([--disable-summary],[Dont print configuration summary to show the build-options])],[
  if test "x$enableval" != "xyes" ; then
    enable_sum="no"
  else
    enable_sum="yes"
  fi],[enable_sum="yes"])
AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug],[Enable debugging mode. Default is disabled.])],[
  if test "x$enableval" = "xyes" ; then
    AC_DEFINE([ENABLE_DEBUG],[1],[Enable debbuging mode])
    CFLAGS="${CFLAGS} -g"
    enable_debug="yes"
    AX_CHECK_COMPILE_FLAG(-fno-omit-frame-pointer, [CFLAGS="${CFLAGS} -fno-omit-frame-pointer"],,,)
  else
    enable_debug="no"
    AX_CHECK_COMPILE_FLAG(-fomit-frame-pointer, [CFLAGS="${CFLAGS} -fomit-frame-pointer"],,,)
  fi],[
  enable_debug="no"
  AX_CHECK_COMPILE_FLAG(-fomit-frame-pointer, [CFLAGS="${CFLAGS} -fomit-frame-pointer"],,,)])
AC_ARG_ENABLE([reqdbg], [AS_HELP_STRING([--enable-reqdbg],[Enable per-request-debugging. Default is disabled.])],[
  if test "x$enableval" = "xyes" ; then
    AC_DEFINE([ENABLE_REQDBG],[1],[Enable per-request-debugging])
    enable_reqdbg="yes"
  else
    enable_reqdbg="no"
  fi],
  enable_reqdbg="no")
AC_CHECK_HEADERS([assert.h], [AC_ARG_ENABLE([assert], [AS_HELP_STRING([--enable-assert],[Enable assertions. Default is disabled.])],[
  if test "x$enableval" = "xyes" ; then
    enable_assert="yes"
  else
    AC_DEFINE([NDEBUG],[1],[Disable assert() macro])
    enable_assert="no"
  fi],
  AC_DEFINE([NDEBUG],[1],[Disable assert() macro])
  enable_assert="no")], [enable_assert="no (not supported)"])
AX_CHECK_COMPILE_FLAG(-fstack-protector, [AC_ARG_ENABLE([stackprot], [AS_HELP_STRING([--disable-stackprot],[Enable StackProtector. Default is enabled.])],[
  if test "x$enableval" = "xyes" ; then
    CFLAGS="${CFLAGS} -fstack-protector"
    stackprot="yes"
  else
    CFLAGS="${CFLAGS} -fno-stack-protector"
    stackprot="no"
  fi],
  CFLAGS="${CFLAGS} -fstack-protector"
  stackprot="yes")], [stackprot="no (not supported)"],,)
AX_CHECK_COMPILE_FLAG(-fexpensive-optimizations, [CFLAGS="${CFLAGS} -fexpensive-optimizations"],,,)
AX_CHECK_COMPILE_FLAG(-O2, [CFLAGS="${CFLAGS} -O2"],,,)

AC_ARG_ENABLE([pthread], [AS_HELP_STRING([--disable-pthread],[Disable POSIX Threads. Default is enable.])],[have_pthreads=no],[AX_PTHREAD([have_pthreads=yes; LIBS="${LIBS} -lpthread"], [have_pthreads=no])])
AC_ARG_ENABLE([evhttp], [AS_HELP_STRING([--disable-evhttp],[Enable libevent httpd. Default is enable.])],[have_libevent=no],[AX_CHECK_LIBRARY([LIBEVENT2], [event2/event.h], [event], [have_libevent=yes; LIBS="${LIBS} -levent"; AC_DEFINE([ENABLE_LIBEVENT],[1],[Enable http service with evhttp])], [have_libevent=no])])

if test "x$have_pthreads" = "xyes"; then
  AC_DEFINE([ENABLE_PTHREADS],[1],[Disable if you dont want to use threads in ncblog])
  AC_DEFINE([MAINTHRD_HEARTBEAT],[1200],[Disable if you dont want to print status messages about the memory consumption])
  AX_TLS([], [AC_MSG_ERROR([*** Need pthread support with thread local storage.])])
fi
if test "x$have_libevent" != "xyes"; then
  AC_MSG_ERROR([*** Libevent not found.])
fi
if test "x$have_libevent" = "xyes"; then
  if test "x$have_pthreads" = "xyes"; then
    AX_CHECK_LIBRARY([LIBEVENT_PTHREADS], [event2/thread.h], [event_pthreads],[LIBS="${LIBS} -levent_pthreads"], [AC_MSG_ERROR([*** Pthread support found, but libevent doesnt support it? Maybe your system is broken ..])])
  fi
fi

AX_CHECK_LIBRARY([LIBM], [math.h], [m], [have_libm=yes; LIBS="${LIBS} -lm"], [AC_MSG_ERROR([*** Unable to find libm])])
if test "x$have_libm" = "xyes"; then
  AC_CHECK_FUNCS([exp2],,[AC_MSG_ERROR([*** Missing math func exp2.])])
fi
AX_LIB_SQLITE3([3.7.13])
if test "x$success" = "xyes"; then
    have_sqlite3="yes"
    LIBS="${LIBS} -lsqlite3"
    AC_DEFINE([ENABLE_DB_SQLITE3],[1],[Build with sqlite3 db-mgmt system])
else
    have_sqlite3="no"
fi

AX_CHECK_COMPILE_FLAG(-ffast-math, [CFLAGS="${CFLAGS} -ffast-math"],,,)
AX_CHECK_LINK_FLAG(--gc-sections, [LDFLAGS="${LDFLAGS} --gc-sections"],,,)
AM_CONDITIONAL([BUILD_PTHREADS],[test x$have_pthreads = xyes])
AM_CONDITIONAL([BUILD_HTTPD],[test x$have_libevent = xyes])

AC_DEFINE([MAX_THREADS],[4],[Maximum worker threads])
AC_DEFINE([ENABLE_SIMPLE_TP],[1],[Simple and minimalistic template system])
AC_DEFINE([USE_BUILTIN_BLOG],[1],[Build the blogging builtin])
AC_DEFINE([USE_BUILTIN_DATE],[1],[Build the date builtin])
AC_DEFINE([USE_BUILTIN_VERSION],[1],[Build the version builtin])
bltns="blog date version"

CFLAGS="${CFLAGS} -std=gnu99 -Wall -Wunused -pedantic -D_GNU_SOURCE"
AC_DEFINE([HAVE_CONFIG], [1], [Do NOT change THIS!])
AC_DEFINE_UNQUOTED([BUILD_HOST], ["${host}"], [build system string])
AC_DEFINE_UNQUOTED([BUILD_TARGET], ["${target}"], [target system string])
AC_DEFINE_UNQUOTED([BUILTIN_FEATURES], ["--enable-stack-prot=${stackprot} --enable-debug=${enable_debug} --enable-reqdbg=${enable_reqdbg} --enable-assert=${enable_assert} --enable-lua=${have_lua} --enable-sqlite3=${have_sqlite3} --enable-event2=${have_libevent} --enable-pthreads=${have_pthreads} --enable-libm=${have_libm}"],[You should not manually change this..])
AC_DEFINE([VERSION], [PACKAGE_VERSION], [package version])

AC_SUBST([AM_CFLAGS])
AC_SUBST([AM_LDFLAGS])
AC_CONFIG_FILES([Makefile src/Makefile src/builtin/Makefile])
AC_OUTPUT

if test "x${target}" = "x"; then
  target="${host}"
fi

if test "x$enable_sum" = "xyes"; then
echo \
"-----------------------------------------
ncblog config options:

  Option                = Value
  Host                  = ${host}
  Build                 = ${build}
  Target                = ${target}
  StackGuard            = ${stackprot}
  Debug mode            = ${enable_debug}
  Request debug         = ${enable_reqdbg}
  Assertions            = ${enable_assert}
  sqlite3               = ${have_sqlite3}
  libevent2             = ${have_libevent}
  lua5                  = ${have_lua}
  pthreads              = ${have_pthreads}
  libm                  = ${have_libm}
  Builtins              = ${bltns}
------------------------------------------

Options used to compile and link:
  Source code location  = ${srcdir}
  Compiler              = ${CC}
  Compiler flags        = ${CFLAGS}
  Linker flags          = ${LIBS} ${LDFLAGS}
  Build path            = ${builddir}
  Install path          = ${prefix}

  See config.h for more options.
-----------------------------------------"
fi
